// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameConfig from "./config/GameConfig";

const {ccclass, property} = cc._decorator;

@ccclass
export default class CameraControl extends cc.Component {


    @property(cc.Node)
    target:cc.Node = null;

    canvasSize:cc.Size = null;
    edgeOffset= 1920
    mapWidth = 3000
    cameraCenter  = null;

    onLoad () {}

    start () {
        this.canvasSize = cc.view.getFrameSize();
        console.log(this.canvasSize)
        this.cameraCenter = cc.v2(this.canvasSize.width / 2, this.canvasSize.height / 2);
    }

    moveCameraX(tempPos) {
        
    }


    lateUpdate(dt) {
        if (GameConfig.isBattle) return //如果战斗中，则开始另一种处理
        // 获取玩家位置
        let playerPos = this.target.convertToWorldSpaceAR(cc.Vec2.ZERO);

        // 获取摄像机位置
        let cameraPos = this.node.position;

        // 计算摄像机应该移动到的位置，使玩家位于屏幕中心
        let desiredPos = cc.v2(playerPos.x - this.cameraCenter.x, playerPos.y);

        // 限制摄像机移动范围
        desiredPos.x = cc.misc.clampf(desiredPos.x, this.cameraCenter.x + this.edgeOffset, this.mapWidth - this.cameraCenter.x - this.edgeOffset);
        desiredPos.y = cc.misc.clampf(desiredPos.y, this.cameraCenter.y + this.edgeOffset, this.mapWidth - this.cameraCenter.y - this.edgeOffset); // 假设地图高度与宽度相同

        // // 更新摄像机位置
        // this.node.position.x = desiredPos.x;
        // this.node.position.y = desiredPos.y;
        this.node.x = desiredPos.x// = playerPos.x;
    }
}
